<template>
  <div class="analysis-menu">
    <div class="analysis-menu-title">分析库</div>
    <div class="analysis-menu-items">
      <div
        class="analysis-menu-item"
        :class="{ active: isActive(subItem.action) }"
        v-for="(subItem, subIndex) in subItems"
        :key="subIndex"
        @click="executeFunction(subItem)"
      >
        <div class="menu-item-icon">
          <IconFont class="icon" :name="subItem.icon" size="2.0833vw" />
          <!-- <img :src="subItem.icon" alt="功能图标" /> -->
        </div>
        <div class="menu-item-text">{{ subItem.text }}</div>
      </div>
    </div>

    <div class="analysis-menu-button">
      <button class="menu-button" @click="clearRecently">撤销</button>
      <button class="menu-button" @click="clearAll">清除全部分析</button>
    </div>

    <div class="panel-container" v-if="currentComponent">
      <component :is="currentComponent" @close-panel="closePanel" />
    </div>
  </div>
</template>

<script setup>
import {
  ESViewShed,
  ESVisibilityAnalysis,
  // ESSunshineAnalysis,
  ESGltfModel,
  ES3DTileset,
  ESVolumeMeasurement,
} from 'earthsdk3'
import { getobjm } from '@/utils/getobjm.js' // 导入场景对象管理器工具
import { useAnalysisStore } from '@/stores/analysisStore.js'
import { onUnmounted, ref } from 'vue'
import VolumetricAnalysis from './components/VolumetricAnalysis.vue'
import SunshineAnalysis from './components/SunshineAnalysis.vue'
const analysisStore = useAnalysisStore()
const objm = getobjm()
const subItems = [
  {
    text: '视域分析',
    icon: 'sy',
    // icon: new URL('@/assets/images/sidebar/analysisLibrary/fxTest.png', import.meta.url).href,
    action: 'createESViewShed',
  },
  {
    text: '通视分析',
    icon: 'ts',
    action: 'createESVisibilityAnalysis',
  },
  {
    text: '日照分析',
    icon: 'rz',
    action: 'createESSunshineAnalysis',
  },
  {
    text: '体积测量',
    icon: 'tj',
    action: 'createESVolumeMeasurement',
  },

  {
    text: '创建建筑',
    icon: 'jz',
    action: 'creatESGltfModel',
  },
  {
    text: '大雁塔',
    icon: 'dyt',
    action: 'createDYT3dTileset',
  },
  {
    text: '白膜城市',
    icon: 'cs',
    action: 'createCity3dTileset',
  },
  {
    text: 'BIM模型',
    icon: 'bim',
    action: 'createBIM3dTileset',
  },
  {
    text: '清空模型',
    icon: 'qk',
    action: 'clearModel',
  },
]
// 当前打开的配置面板组件
const currentComponent = ref(null)

// 当前激活的功能
const activeAction = ref(null)

// 检查功能是否激活
const isActive = (action) => {
  return activeAction.value === action && currentComponent.value !== null
}

// 动作与组件的映射
const actionToComponent = {
  VolumetricAnalysis: VolumetricAnalysis,
  SunshineAnalysis: SunshineAnalysis,
}

// 功能执行方法
const executeFunction = (subItem) => {
  // 如果点击的是当前激活的功能，则关闭面板
  if (activeAction.value === subItem.action && currentComponent.value !== null) {
    currentComponent.value = null
    activeAction.value = null
    return
  }

  // 设置当前激活的功能
  activeAction.value = subItem.action

  switch (subItem.action) {
    case 'createESViewShed':
      createESViewShed()
      break
    case 'createESVisibilityAnalysis':
      createESVisibilityAnalysis()
      break
    case 'createESSunshineAnalysis':
      createESSunshineAnalysis()
      break
    case 'createESVolumeMeasurement':
      createESVolumeMeasurement()
      break
    case 'creatESGltfModel':
      creatESGltfModel()
      break
    case 'createDYT3dTileset':
      createDYT3dTileset()
      break
    case 'createCity3dTileset':
      createCity3dTileset()
      break
    case 'createBIM3dTileset':
      createBIM3dTileset()
      break
    case 'clearModel':
      clearModel()
      break
    default:
      console.log(`未知功能: ${subItem.action}`)
  }
}

// 定义emit，用于通知父组件关闭子菜单
const emit = defineEmits(['close-submenu'])
// 关闭配置面板
const closePanel = () => {
  currentComponent.value = null
  activeAction.value = null
  emit('close-submenu') //关闭子菜单
}

// 视域分析
const createESViewShed = () => {
  const sceneObjectESViewShed = objm.createSceneObject(ESViewShed)
  if (!sceneObjectESViewShed) return
  sceneObjectESViewShed.editing = true
  sceneObjectESViewShed.editingChanged.disposableWeakOn(() => {
    if (sceneObjectESViewShed && sceneObjectESViewShed.editing === false) {
      const position = sceneObjectESViewShed.position
      const a = position[0] === 0 && position[1] === 0
      setTimeout(() => {
        if (!a) {
          analysisStore.createAnalysisData.push(sceneObjectESViewShed)
        }
      }, 300)
    }
  })
  emit('close-submenu')
}
//通视分析
const createESVisibilityAnalysis = () => {
  const sceneObjectESVisibilityAnalysis = objm.createSceneObject(ESVisibilityAnalysis)
  if (!sceneObjectESVisibilityAnalysis) return
  sceneObjectESVisibilityAnalysis.editing = true

  sceneObjectESVisibilityAnalysis.editingChanged.disposableWeakOn(() => {
    if (sceneObjectESVisibilityAnalysis && sceneObjectESVisibilityAnalysis.editing === false) {
      const pos = sceneObjectESVisibilityAnalysis.points?.length
      setTimeout(() => {
        if (pos && pos >= 2) {
          analysisStore.createAnalysisData.push(sceneObjectESVisibilityAnalysis)
        }
      }, 300)
    }
  })
  emit('close-submenu')
}

// 日照分析
const createESSunshineAnalysis = () => {
  currentComponent.value = actionToComponent['SunshineAnalysis']
}

//体积测量
const createESVolumeMeasurement = () => {
  currentComponent.value = actionToComponent['VolumetricAnalysis']
}

//创建建筑
const creatESGltfModel = () => {
  const sceneObjectESGltfModel = objm.createSceneObject(ESGltfModel)
  if (!sceneObjectESGltfModel) return
  sceneObjectESGltfModel.position = [106.58608806440223, 29.568585630104735, 123.01601257666258]
  analysisStore.createModel.push(sceneObjectESGltfModel)
  //创建完成 飞入
  sceneObjectESGltfModel.czmModelReadyEvent.don((model) => {
    sceneObjectESGltfModel.flyTo(1)
  })
  emit('close-submenu')
}

//大雁塔
const createDYT3dTileset = () => {
  const sceneObjectES3DTileset = objm.createSceneObject(ES3DTileset)
  if (!sceneObjectES3DTileset) return
  // url服务地址
  sceneObjectES3DTileset.url = 'https://www.pipeser.com/cesiumlab/G7qC5vBk/tileset.json'
  analysisStore.createModel.push(sceneObjectES3DTileset)
  // 加载完成事件
  sceneObjectES3DTileset.tilesetReady.don(() => {
    sceneObjectES3DTileset.flyTo()
  })
  emit('close-submenu')
}

//白膜城市
const createCity3dTileset = () => {
  const sceneObjectES3DTileset = objm.createSceneObject(ES3DTileset)
  if (!sceneObjectES3DTileset) return
  // url服务地址
  sceneObjectES3DTileset.url = 'https://www.pipeser.com/cesiumlab/7hfKb4S4/tileset.json'
  sceneObjectES3DTileset.materialMode = 'technology' //科技感
  analysisStore.createModel.push(sceneObjectES3DTileset)
  // 加载完成事件
  sceneObjectES3DTileset.tilesetReady.don(() => {
    sceneObjectES3DTileset.flyTo()
  })
  emit('close-submenu')
}
//BIM模型
const createBIM3dTileset = () => {
  const sceneObjectES3DTileset = objm.createSceneObject(ES3DTileset)
  if (!sceneObjectES3DTileset) return
  // url服务地址
  sceneObjectES3DTileset.url = 'https://www.pipeser.com/cesiumlab/jgYgbe3y/tileset.json'
  analysisStore.createModel.push(sceneObjectES3DTileset)
  // 加载完成事件
  sceneObjectES3DTileset.tilesetReady.don(() => {
    sceneObjectES3DTileset.flyTo()
  })
  emit('close-submenu')
}

//撤回最近一次分析
const clearRecently = () => {
  if (analysisStore.hasCreateAnalysisData) {
    // 获取数组最后一项（最近创建的分析）
    const recently = analysisStore.createAnalysisData.pop()
    // 销毁该分析对象
    objm.destroySceneObject(recently)
  }
}

//清除全部分析
const clearAll = () => {
  // 清除所有已保存的分析
  if (analysisStore.hasCreateAnalysisData) {
    analysisStore.createAnalysisData.forEach((item) => {
      if (item) objm.destroySceneObject(item)
    })
    analysisStore.createAnalysisData = []
  }
}

const clearModel = () => {
  if (analysisStore.hasCreatecreateModel) {
    analysisStore.createModel.forEach((item) => {
      if (item) objm.destroySceneObject(item)
    })
  }
  analysisStore.createModel = []
  emit('close-submenu')
}
</script>

<style lang="scss" scoped>
.analysis-menu {
  width: 15.625vw /* 300px -> 15.625vw */;
  background-color: rgba(0, 8, 26, 0.8);
  border-radius: 0.4167vw /* 8px -> .4167vw */;
  box-shadow: 0 0 0.5208vw /* 10px -> .5208vw */ rgba(0, 195, 255, 0.5);
  border: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.6);
  padding: 0.5208vw /* 10px -> .5208vw */;
  z-index: 2;
  .analysis-menu-title {
    font-size: 0.8333vw /* 16px -> .8333vw */;
    color: #00c3ff;
    font-weight: bold;
    padding-bottom: 0.5208vw /* 10px -> .5208vw */;
    border-bottom: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.3);
  }

  .analysis-menu-items {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 0.7813vw /* 15px -> .7813vw */;
    padding: 0.5208vw /* 10px -> .5208vw */ 0;
    flex-wrap: wrap;
    max-height: 10.4167vw /* 200px -> 10.4167vw */;
    overflow: auto;
    // 自定义滚动条样式
    &::-webkit-scrollbar {
      width: 0.4167vw /* 8px -> .4167vw */; // 滚动条宽度
    }

    &::-webkit-scrollbar-track {
      background: rgba(0, 0, 0, 0.3); // 滚动条轨道背景
      border-radius: 0.2083vw /* 4px -> .2083vw */; // 轨道圆角
    }

    &::-webkit-scrollbar-thumb {
      background: rgba(0, 195, 255, 0.5); // 滚动条滑块颜色
      border-radius: 0.2083vw /* 4px -> .2083vw */; // 滑块圆角
    }
    .analysis-menu-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      gap: 0.5208vw /* 10px -> .5208vw */;
      width: 4.1667vw /* 80px -> 4.1667vw */;
      height: 4.1667vw /* 80px -> 4.1667vw */;
      cursor: pointer;
      border-radius: 0.2083vw /* 4px -> .2083vw */;
      &:hover {
        background-color: rgba(0, 195, 255, 0.2);
        .menu-item-icon {
          .icon {
            scale: 0.8;
          }
        }
      }
      &.active {
        background-color: rgba(0, 195, 255, 0.3);
        border: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.8);

        .menu-item-text {
          color: #00c3ff;
          font-weight: bold;
        }
      }

      .menu-item-icon {
        .icon {
          width: 2.0833vw /* 40px -> 2.0833vw */;
          height: 2.0833vw /* 40px -> 2.0833vw */;
          object-fit: contain;
          transition: all 0.5s;
        }
      }
      .menu-item-text {
        height: 0.8333vw /* 16px -> .8333vw */;
        line-height: 0.8333vw /* 16px -> .8333vw */;
        font-size: 0.625vw /* 12px -> .625vw */;
        color: #ffffff;
        text-align: center;
      }
    }
  }
  .analysis-menu-button {
    padding-top: 0.5208vw /* 10px -> .5208vw */;
    border-top: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.3);
    display: flex;
    justify-content: space-between;
    align-items: center;
    .menu-button {
      background-color: rgba(0, 195, 255, 0.2);
      border: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.6);
      color: #00c3ff;
      padding: 0.2604vw /* 5px -> .2604vw */ 0.4167vw /* 8px -> .4167vw */;
      border-radius: 0.2083vw /* 4px -> .2083vw */;
      font-size: 12px;
      cursor: pointer;
      &:hover {
        background-color: rgba(0, 195, 255, 0.4);
      }
    }
  }
  .panel-container {
    position: absolute;
    left: 16.1458vw /* 310px -> 16.1458vw */;
    bottom: 0;
  }
}
</style>
